<?php

namespace App\Model\Front;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
 


    public $table = 'product_categorys';
    
    protected $fillable = ['name','slug','parent_id','is_floor','floor_color'];

    public function products()
    {
        return $this->hasMany('App\Model\Admin\Product', 'cate_id', 'id');
    }

    public function subcate()
    {
        return $this->hasMany('App\Model\Front\Category', 'parent_id', 'id');
    }

 
    /**
     * 获取首页顶级分类推荐楼层
     * @return Collections
     */
    public static function topFloor()
    {
        $topCateFloor = Category::where('is_floor',true)->where('parent_id','0')->get();
        return $topCateFloor;
    }

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function child()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    public function parenPath($catId)
    {
        static $ret = array(); 
        $info = Category::select('id', 'name', 'slug','parent_id')->find($catId);
        $ret[] = $info;
        if($info->parent_id > 0){
            $this->parenPath($info->parent_id);
        } 
        return $ret;
    }
 
    

    public function getRouteKeyName()
    {
        return 'slug';
    }

}
